package com.by.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.entity.Category;
import com.by.entity.Goods;
import com.by.service.CategoryService;
import com.by.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author 黄佳辉
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/v1/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/query")
    public Result<List<Category>> query() {
        List<Category> categoryList = categoryService.getCategoryList();
        return Result.success("类别列表",categoryList);
    }

    @GetMapping("/info/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success("getCategoryById", category);
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody Category category) {
        boolean saveFlag = categoryService.save(category);
        System.out.println(saveFlag);
        return saveFlag ? Result.success("保存成功") : Result.fail("保存失败");
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody Category category){
        Boolean updateFlag= categoryService.updateById(category);
        return updateFlag ? Result.success("修改成功") : Result.fail("修改失败");
    }

    // 删除该类别下的所有类别
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        Boolean deleteFlag = categoryService.removeByIdWithChildren(id);
        return deleteFlag ? Result.success("删除成功") : Result.fail("删除失败");
    }
}
